/* Convert a byte array to hex */

function binaryToHex(binary) {
    const u8 = new Uint8Array(binary);
    let str = '';

    for (const v of u8) {
        str += ('00' + v.toString(16)).slice(-2);
    }

    return str;
}


/* SHA-256 hash the password before touching the wire or disk. Encode as per
 * in the backend */

window.hashPassword = function(password) {
    const buffer = new TextEncoder('utf-8').encode(password);
    return crypto.subtle.digest('SHA-256', buffer).then(binaryToHex);
};
